package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.CommentManagerService;
import com.woniu.p2p.entity.Invest;
import com.woniu.p2p.entity.InvestComment;
import com.woniu.p2p.vo.InvestAndCommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("/myCommentManager")
public class CommentManagerController {

    @Autowired
    CommentManagerService commentManagerService;

    @RequestMapping("/getCommentDetail")
    public String getCommentDetail(Map map, String commentstatus,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date predate,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date nextdate,
                                   String commentkeyword,
                                   Integer pagenum,
                                   Integer pagesize){
        pagenum=pagenum==null?1:pagenum;
        pagesize=pagesize==null?3:pagesize;
        PageInfo<InvestAndCommentVo> pageInfo = commentManagerService.findAllInvestComment(pagenum, pagesize, commentstatus, predate, nextdate, commentkeyword);
        map.put("commentManagerDetail",pageInfo);
        map.put("commentstatus",commentstatus);
        map.put("predate",predate);
        map.put("nextdate",nextdate);
        map.put("commentkeyword",commentkeyword);
        map.put("pagesize",pagesize);

        List<Invest> allInvest = commentManagerService.findAllInvest();
        map.put("insertInvest",allInvest);

        List<InvestComment> list = commentManagerService.selectallcomment();
        ArrayList<Integer> ids = new ArrayList<>();
        for(InvestComment investComment :list){
            ids.add(investComment.getCommentId());
        }
        map.put("ids",ids);

        return "content/commentManager";
    }


    /**
     * 导出
     */
    @RequestMapping("/outComment")
    public void exportRepository(int[] id,
                                 HttpServletResponse response) throws IOException {
        System.out.println(Arrays.toString(id));
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");


        List<InvestAndCommentVo> list = commentManagerService.findAllComments(id);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, InvestAndCommentVo.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();
    }


    @RequestMapping("/getcomdetail")
    @ResponseBody
    public Invest getinvdetail(int id){
        InvestComment allComment = commentManagerService.findAllComment(id);
        Integer investId = allComment.getInvestId();
        Invest allInvestById = commentManagerService.findAllInvestById(investId);
        return allInvestById;
    }


    @RequestMapping("/insertComment")
    public int insertAfterloans(int insertInvest,String commentName,String commentContext,@DateTimeFormat(pattern = "yyyy-MM-dd") Date commentDate
                                ){
        InvestComment investComment = new InvestComment(commentName, commentContext, commentDate, insertInvest);
        commentManagerService.insertComment(investComment);
        return 1;
    }


    @RequestMapping("/delonecomment")
    public int deloneafterloans(int id){
        commentManagerService.deleteCommentById(id);
        return 1;
    }


    @RequestMapping("/delallcomment")
    public int deloneafterloans(int[] id){
        for (int i = 0; i <id.length ; i++) {
            commentManagerService.deleteCommentById(id[i]);
        }
        return 1;
    }



}
